package com.sqfw.project.knowledge.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.sqfw.common.utils.DateUtils;
import com.sqfw.common.utils.SecurityUtils;
import com.sqfw.framework.security.LoginUser;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.project.knowledge.domain.KlCollectSort;
import com.sqfw.project.knowledge.domain.KlContentCollect;
import com.sqfw.project.knowledge.mapper.KlCollectSortMapper;
import com.sqfw.project.knowledge.mapper.KlContentCollectMapper;
import com.sqfw.project.knowledge.service.IKlContentCollectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 知识收藏Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-15
 */
@Service
public class KlContentCollectServiceImpl implements IKlContentCollectService {
    @Autowired
    private KlContentCollectMapper klContentCollectMapper;

    @Autowired
    private KlCollectSortMapper klCollectSortMapper;


    /**
     * 查询知识收藏列表
     * 
     * @param klContentCollect 知识收藏
     * @return 知识收藏
     */
    @Override
    public List<KlContentCollect> selectKlContentCollectList(KlContentCollect klContentCollect) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        klContentCollect.setUserId(loginUser.getUserId().toString());
        List<KlContentCollect> collects = klContentCollectMapper.selectKlContentCollectList(klContentCollect);
        if (CollectionUtils.isEmpty(collects)) {
            return collects;
        }
        KlCollectSort klCollectSort = new KlCollectSort();
        klCollectSort.setUserId(loginUser.getUserId().toString());
        List<KlCollectSort> sortList = klCollectSortMapper.selectKlCollectSortList(klCollectSort);
        for (KlContentCollect collect : collects) {
            for (KlCollectSort sort : sortList) {
                if (collect.getSortId().equals(sort.getAutoId())) {
                    collect.setSort(sort);
                }
            }
        }
        return collects;
    }

    /**
     * 新增知识收藏
     * 
     * @param klContentCollect 知识收藏
     * @return 结果
     */
    @Override
    public AjaxResult insertKlContentCollect(KlContentCollect klContentCollect) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        klContentCollect.setUserId(loginUser.getUserId().toString());
        KlContentCollect collect = klContentCollectMapper.selectKlContentCollect(klContentCollect);
        if (collect != null) {
            return AjaxResult.error("该知识已经收藏了");
        }

        klContentCollect.setCreateTime(DateUtils.getNowDate());
        int i = klContentCollectMapper.insertKlContentCollect(klContentCollect);
        if (i > 0) {
            return AjaxResult.success("收藏成功");
        }
        return AjaxResult.error("收藏失败");
    }

    /**
     * 删除知识收藏---根据id删除
     * @param id
     * @return
     */
    @Override
    public int deleteKlContentCollectById(String id) {
        return klContentCollectMapper.deleteKlContentCollectById(id);
    }


    // ------------------------------收藏分类  开始-------------------------------------------

    /**
     * 添加收藏分类，或者新增子分类
     * @param klCollectSort
     * @return
     * */
    @Override
    public AjaxResult addKlCollectSort(KlCollectSort klCollectSort) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        klCollectSort.setUserId(loginUser.getUserId().toString());
        klCollectSort.setSortName(klCollectSort.getSortName().trim());
        KlCollectSort oldKlCollectSort = klCollectSortMapper.selectKlCollectSort(klCollectSort);
        if (oldKlCollectSort != null) {
            return AjaxResult.error("分类名称已存在");
        }
        klCollectSort.setCreateTime(new Date());
        int i = klCollectSortMapper.insertKlCollectSort(klCollectSort);
        if (i > 0) {
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 修改分类名称
     * @param klCollectSort
     * @return
     * */
    @Override
    public AjaxResult updateKlCollectSort(KlCollectSort klCollectSort) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        klCollectSort.setUserId(loginUser.getUserId().toString());
        klCollectSort.setSortName(klCollectSort.getSortName().trim());
        KlCollectSort oldKlCollectSort = klCollectSortMapper.selectKlCollectSort(klCollectSort);
        if (oldKlCollectSort != null) {
            // 同一个id，移动目录；不是同一个id，修改名称
            if (!oldKlCollectSort.getAutoId().equals(klCollectSort.getAutoId())) {
                return AjaxResult.error("分类名称已存在");
            }
        }
        klCollectSort.setUpdateTime(new Date());
        int i = klCollectSortMapper.updateKlCollectSort(klCollectSort);
        if (i > 0) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 查询收藏分类
     * @return
     * */
    @Override
    public List<KlCollectSort> getKlCollectSortList() {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        KlCollectSort klCollectSort = new KlCollectSort();
        klCollectSort.setUserId(loginUser.getUserId().toString());
        List<KlCollectSort> sortList = klCollectSortMapper.selectKlCollectSortList(klCollectSort);
        if (CollectionUtils.isEmpty(sortList)) {
            return new ArrayList<>();
        }
        // 一级
        List<KlCollectSort> oneSortList = new ArrayList<>();
        for (KlCollectSort sort : sortList) {
            if ("0".equals(sort.getPid())) {
                sort.setValue(sort.getAutoId());
                sort.setLabel(sort.getSortName());
                oneSortList.add(sort);
            }
        }
        for (KlCollectSort oneSort : oneSortList) {
            findChildren(oneSort, sortList);
        }
        return oneSortList;
    }

    /**
     * 删除收藏分类
     * @param klCollectSort
     * @return
     * */
    @Override
    public AjaxResult deleteKlCollectSort(KlCollectSort klCollectSort) {
        // 查询是否有子分类
        KlCollectSort sort = new KlCollectSort();
        sort.setPid(klCollectSort.getAutoId());
        List<KlCollectSort> sortList = klCollectSortMapper.selectKlCollectSortList(sort);
        if (!CollectionUtils.isEmpty(sortList)) {
            return AjaxResult.error("请先删除子分类!");
        }
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        KlContentCollect klContentCollect = new KlContentCollect();
        klContentCollect.setSortId(klCollectSort.getAutoId());
        klContentCollect.setUserId(loginUser.getUserId().toString());
        List<KlContentCollect> collects = klContentCollectMapper.selectKlContentCollectList(klContentCollect);
        if (!CollectionUtils.isEmpty(collects)) {
            return AjaxResult.error("该分类包含收藏的知识，请先清空收藏知识!");
        }
        int i = klCollectSortMapper.deleteKlCollectSortByAutoId(klCollectSort.getAutoId());
        if (i > 0) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }


    /**
     * 使用递归，进行分类，由于级数太多了
     * */
    public KlCollectSort findChildren(KlCollectSort sort, List<KlCollectSort> sortList) {
        for (KlCollectSort klCollectSort : sortList) {
            if(sort.getAutoId().equals(klCollectSort.getPid())) {
                if (sort.getChildren() == null) {
                    sort.setChildren(new ArrayList<KlCollectSort>());
                }
                klCollectSort.setValue(klCollectSort.getAutoId());
                klCollectSort.setLabel(klCollectSort.getSortName());
                sort.getChildren().add(findChildren(klCollectSort, sortList));
            }
        }
        return sort;
    }


}
